package at.fhj.itm;

/**
 * Class which implements a Queue and provides methods to insert and remove 
 * new items and check the state of the queue (isEmpty, etc.).
 * 
 * @author Reinhard Handler
 * @version 1.0
 *
 */
public class Queue {
	
  /** maximum amount of items in queue */	
  private int maxSize;

  /** array to save items */
  private long[] queArray;

  /** index of first element in queue */
  private int front;

  /** index of last element in queue */
  private int rear;

  /** current amount of items in queue */
  private int nItems;

  /**
   * Constructs a new queue of given size.
   * @param s	maximum elements in queue
   */
  public Queue(int s) {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }

  /**
   * puts a item ad end of the queue
   * @param j	item to add to queue
   * @throws ArrayIndexOutOfBoundsException if queue is full
   */
  public void insert(long j) {
	if ( isFull() )
		throw new ArrayIndexOutOfBoundsException( "queue is full" );
    if (rear == maxSize - 1) // deal with wraparound
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; 
  }

  /**
   * removes first item in queue and return it.
   * @return	removed/first item in queue
   * @throws ArrayIndexOutOfBoundsException if queue is full
   */
  public long remove() {
	if ( isEmpty() ) {
		throw new ArrayIndexOutOfBoundsException( "queue is empty" );
	}
    long temp = queArray[front++]; // get value and incr front
    if (front == maxSize) // deal with wraparound
      front = 0;
    nItems--; // one less item
    return temp;
  }

  /**
   * get the first item in queue
   * @return	first item in queue
   */
  public long peekFront() {
    return queArray[front];
  }

  /**
   * checks if queue is empty
   * @return	true if queue is empty
   */
  public boolean isEmpty() {
    return (nItems == 0);
  }

  /**
   * checks if queue is full
   * @return	true if queue is full
   */
  public boolean isFull() {
    return (nItems >= maxSize);
  }

  /**
   * gets the current amount of items in queue
   * @return	current amount of items in queue
   */
  public int size() {
    return nItems;
  }
}
           
         